# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
from baidu import items

class BaiduPipeline(object):
    def __init__(self):
         self.serverIP = '127.0.0.1'
         self.serverPort = 54545
         self.serverDB = 'baidu'
         self.client = pymongo.MongoClient(self.serverIP, self.serverPort)
         self.db = self.client[self.serverDB]
         self.html = self.db['html']
         self.pic = self.db['pic']
         self.mdd = self.db['mdd']
         self.hotel = self.db['hotel']
         self.comment = self.db['comment']
         self.youji = self.db['youji']

    def addMDD(self, mdd):
        if self.mdd.find_one({'cname': mdd['cname'], 'url': mdd['url']}):
            return
        self.mdd.save(dict(mdd))
    def addMayouji(self, youji):
        if self.youji.find_one({'url': youji['url']}):
            return
        self.youji.save(dict(youji))
    def process_item(self, item, spider):
        if isinstance(item, items.BaiduMDD):
            self.addMDD(item)
        elif isinstance(item, items.BaiduYouji):
            self.addMayouji(item)
        else:
            print "can not deal the item ", item
